package com.eascs.web.o2o.web.controller;

import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.o2o.web.entity.BankLimit;
import com.eascs.web.o2o.web.service.BankLimitService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
@RequestMapping("/banklimit")
public class BankLimitRestController extends BaseRestController {
	@Autowired
	private BankLimitService bankLimitService;

	@RequestMapping("/add/index")
	public HtmlRestResult addIndex() {
		return HtmlRestResult.popHtmlFragment("/banklimit/limit_add");
	}

	@RequestMapping("/update/index")
	public HtmlRestResult updateIndex(String id) {
		BankLimit bankLimit = bankLimitService.getLimitById(id);
		ViewContext.put("bl", bankLimit);
		return HtmlRestResult.popHtmlFragment("/banklimit/limit_add");
	}

	@RequestMapping(value = "/delete")
	public BaseRestResult<String> delete(String id) {
		BankLimit bankLimit = bankLimitService.getLimitById(id);
		bankLimit.setIsDelete("Y");
		int effectRow = bankLimitService.update(bankLimit);
		if (effectRow <= 0) {
			return BaseRestResult.warn("删除失败！");
		}
		return BaseRestResult.info("删除成功").andRefresh();
	}

	@RequestMapping(value = "/save")
	public BaseRestResult<String> saveOrUpdate(String bankType, BigDecimal limit, String remark, String id) {
		if (limit == null) {
			return BaseRestResult.warn("银行额度不能为空！");
		}

		User loginUser = UserHolder.getUser();
		if (StringUtils.isBlank(id)) {// 添加
			if (StringUtils.isBlank(bankType)) {
				return BaseRestResult.warn("银行名称不能为空！");
			}
			BankLimit bankLimit = bankLimitService.getLimit(bankType);
			if (bankLimit != null) {
				return BaseRestResult.warn("请勿重复添加！");
			}
			BankLimit bl = new BankLimit();
			bl.setBankName(bankType);
			bl.setLimit(limit);
			bl.setCreatorNo(loginUser.getNo());
			bl.setCreatorName(loginUser.getCname());
			bl.setCreateTime(new Date());
			bl.setUpdatorNo(loginUser.getNo());
			bl.setUpdatorName(loginUser.getCname());
			bl.setUpdateTime(new Date());
			bl.setIsDelete("N");
			bl.setRemark(remark);
			BankLimit saveLimit = bankLimitService.insert(bl);
			if (saveLimit == null) {
				return BaseRestResult.warn("添加失败！");
			}
			return BaseRestResult.info("添加成功").andRefresh();
		} else {// 修改
			BankLimit bankLimit = bankLimitService.getLimitById(id);
			bankLimit.setLimit(limit);
			bankLimit.setUpdatorNo(loginUser.getNo());
			bankLimit.setUpdatorName(loginUser.getCname());
			bankLimit.setUpdateTime(new Date());
			bankLimit.setRemark(remark);
			int effectRow = bankLimitService.update(bankLimit);
			if (effectRow <= 0) {
				return BaseRestResult.warn("修改失败！");
			}
			return BaseRestResult.info("修改成功").andRefresh();
		}
	}
}
